<template>
  <keep-alive>
    <component :is="currentView"></component>
  </keep-alive>
</template>
<script>
import Axios from 'axios'
import MainView from '@/views/MainView'
import { mapMutations } from 'vuex'

const BASEURL = {
  initData: '/api/system/initData',
  userInfo: 'GetUserInfo',
  userMenu: 'GetUserMenu',
  kvConfig: 'GetKeyValueConfig'
}
export default {
  name: 'ready',
  data () {
    return {
      currentView: null
    }
  },
  created () {
    setTimeout(() => {
      this.initializeSystem()
    }, 50)
  },
  methods: {
    ...mapMutations(['initializeMenu', 'initializeDictionary', 'setUserInfo']),
    /**
         * 初始化系统数据
         * 1. 字典
         * 2. 菜单(权限)
         * 3. 登录用户信息
         */
    initializeSystem () {
      let loadingFlag = this.$loading({ fullscreen: true, text: this.$t('tip.systemLoading') })

      let baseParamsUrl = `/ajax.html?sys_method=`

      Axios.all([this.$axios.post(`${baseParamsUrl}${BASEURL.userInfo}`), this.$axios.post(`${baseParamsUrl}${BASEURL.userMenu}`), this.$axios.post(`${baseParamsUrl}${BASEURL.kvConfig}`)])
        .then(Axios.spread((userInfo, userMenu, userDict) => {
          this.initializeDictionary(userDict)
          this.initializeMenu(userMenu)
          this.setUserInfo(userInfo)

          // 判断是否有首页访问权限，如果没有则跳转到路由的第一个子菜单中
          let currentSysMenu = this.$store.state.app.sysMenu
          let mayBeHomeMenuItem = currentSysMenu[0]

          if (mayBeHomeMenuItem.children && mayBeHomeMenuItem.children.length > 0) this.$router.replace(mayBeHomeMenuItem.children[0].menuurl)

          this.currentView = MainView
        }))
        .finally(() => {
          loadingFlag.close()
        })

      // this.$axios
      //   .post(BASEURL.initData)
      //   .then(resp => {
      //     let respData = resp.data
      //     this.initializeMenu(respData.menu)
      //     this.initializeDictionary(respData.dict)
      //     this.setUserInfo(respData.user)
      //     this.currentView = MainView
      //   })
      //   .finally(() => {
      //     loadingFlag.close()
      //   })
    }
  }
}
</script>
